#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
功能测试管理平台API快速测试脚本
"""

import requests
import json
import time
from datetime import datetime


def test_api_quick():
    """快速API测试"""
    base_url = "http://127.0.0.1:5000/api/test-management"
    
    print("🚀 功能测试管理平台API快速测试")
    print("=" * 50)
    
    # 1. 健康检查
    print("1. 健康检查...")
    try:
        response = requests.get(f"{base_url}/health")
        if response.status_code == 200:
            print("   ✅ 健康检查通过")
        else:
            print(f"   ❌ 健康检查失败: {response.status_code}")
    except Exception as e:
        print(f"   ❌ 健康检查异常: {str(e)}")
        return False
    
    # 2. 获取用例库列表
    print("2. 用例库列表...")
    try:
        response = requests.get(f"{base_url}/libraries?source=ones")
        if response.status_code == 200:
            data = response.json()
            count = len(data.get("data", []))
            print(f"   ✅ 获取到{count}个用例库")
        else:
            print(f"   ❌ 获取用例库失败: {response.status_code}")
    except Exception as e:
        print(f"   ❌ 获取用例库异常: {str(e)}")
    
    # 3. 同步状态
    print("3. 同步状态...")
    try:
        response = requests.get(f"{base_url}/sync/status")
        if response.status_code == 200:
            data = response.json()
            if data.get("success"):
                print("   ✅ 同步状态正常")
            else:
                print(f"   ⚠️ 同步状态: {data.get('data', {})}")
        else:
            print(f"   ❌ 获取同步状态失败: {response.status_code}")
    except Exception as e:
        print(f"   ❌ 获取同步状态异常: {str(e)}")
    
    # 4. 创建测试迭代
    print("4. 创建测试迭代...")
    try:
        iteration_data = {
            "name": f"快速测试迭代_{int(time.time())}",
            "description": "快速API测试创建的迭代",
            "owner": "API测试",
            "version": "v1.0.0"
        }
        response = requests.post(f"{base_url}/iterations", 
                               json=iteration_data,
                               headers={"Content-Type": "application/json"})
        if response.status_code == 200:
            data = response.json()
            if data.get("success"):
                iteration_id = data["data"]["id"]
                print(f"   ✅ 创建迭代成功，ID: {iteration_id}")
                
                # 5. 获取迭代详情
                print("5. 获取迭代详情...")
                detail_response = requests.get(f"{base_url}/iterations/{iteration_id}")
                if detail_response.status_code == 200:
                    print("   ✅ 获取迭代详情成功")
                else:
                    print(f"   ❌ 获取迭代详情失败: {detail_response.status_code}")
            else:
                print(f"   ❌ 创建迭代失败: {data.get('message')}")
        else:
            print(f"   ❌ 创建迭代请求失败: {response.status_code}")
    except Exception as e:
        print(f"   ❌ 创建迭代异常: {str(e)}")
    
    print("\n🎯 快速测试完成！")
    return True


if __name__ == "__main__":
    test_api_quick() 